package com.freeSoft;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.ResultSetMetaData;
import java.util.Properties;


public class CreateAction {
	private String tableName;
	private Properties prop;
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	

	ResultSetMetaData rs;
 
	public ResultSetMetaData getRs() {
		return rs;
	}
	public void setRs(ResultSetMetaData rs) {
		this.rs = rs;
	}
	public void generateAcion(){
		String actionPath = prop.getProperty(Util.PROP_ACTION_PATH);
		String beanPath = prop.getProperty(Util.PROP_BEAN_PATH);
		String servicePath = prop.getProperty(Util.PROP_SERVICE_PATH);
		String outputDir = Util.getOutputDir(prop);
		String projactName = prop.getProperty(Util.PROJECT_NAME);
		String absolutePath = outputDir+ projactName + File.separator+ projactName+ Util.CORE_NAME +File.separator+ 
		"src"+File.separator+"main"+File.separator+"java"+File.separator+Util.javaPackToFilePath(actionPath);
		File f = new File(absolutePath);
		if ( !f.exists()){
			f.mkdirs();
		}
 
		String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
		beanName = Util.changeName(beanName);
 
		File file = new File(absolutePath+File.separator+beanName+"Action.java");
		try {
			PrintWriter out	   = new PrintWriter(new BufferedWriter( new FileWriter(file)));
			out.println("package "+actionPath +";\r\n");
			out.println();
			out.println("import com.opensymphony.xwork2.ActionSupport;");
			out.println();
			beanName = Util.changeName(beanName);
			out.println("import "+beanPath+"."+beanName+";");
			out.println("import "+ servicePath+"."+Util.changeInitialToUpper(beanName)+"Service;");
			
			createFieldAndMethod(out);
			
			StringBuffer executeBuf = new StringBuffer();
			executeBuf.append("	@Override\r\n");
			executeBuf.append("	public String execute() throws Exception {\r\n");
			executeBuf.append("		return SUCCESS;");
			executeBuf.append("	}");
			
			StringBuffer deleteBuf = new StringBuffer();
			deleteBuf.append("	public String delete"+ beanName +"() throws Exception {\r\n");
			deleteBuf.append("		"+beanName+"Service.deleteUser("+beanName+"Id);");
			deleteBuf.append("		return SUCCESS;");
			deleteBuf.append("	}");
			
			StringBuffer insertBuf = new StringBuffer();
			insertBuf.append("	public String insert"+ Util.changeInitialToUpper(beanName) +"() throws Exception {\r\n");
			insertBuf.append( getBeanValueMethod( beanName));
			insertBuf.append("		return SUCCESS;");
			insertBuf.append("	}");
			
			StringBuffer updateBuf = new StringBuffer();
			updateBuf.append("	public String update"+Util.changeInitialToUpper(beanName) +"() throws Exception {\r\n");
			updateBuf.append( getBeanValueMethod( beanName));
			updateBuf.append("		return SUCCESS;");
			updateBuf.append("	}");
			out.println( executeBuf.toString() );
			out.println( deleteBuf.toString() );
			out.println( insertBuf.toString() );
			out.println( updateBuf.toString() );
			out.flush();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	private StringBuffer getBeanValueMethod(String beanName){
		StringBuffer buf = new StringBuffer();
		String lowerBeanName = Util.changeInitialToLower(beanName);
		buf.append(" 	" +Util.changeInitialToUpper(beanName)+"  "+ lowerBeanName +"; \r\n");
		try{
			
		for(int i=1;i<=rs.getColumnCount();i++){
			String typeName = rs.getColumnTypeName(i);
			String fieldName = rs.getColumnName(i);
			String fieldType =Util.changeJDBCTypeToJavaType(typeName);
			if ( fieldType.equals(""))
				fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
			String tmpFieldName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1, fieldName.length());
			if ( tmpFieldName.toLowerCase().equals("id"))
				buf.append("	"+lowerBeanName+".set"+tmpFieldName+"("+beanName+"Id );\r\n");
			else
				buf.append("	"+lowerBeanName+".set"+tmpFieldName+"("+fieldName +");\r\n");
		 
		}
		}catch(Exception e){
		}
		return buf;
	}
	private void createFieldAndMethod(PrintWriter out){
			String beanName = tableName.substring(0, 1).toUpperCase()+tableName.substring(1, tableName.length());
			beanName = Util.changeName(beanName);
			try {
				StringBuffer fieldArea = new StringBuffer();
				for(int i=1;i<=rs.getColumnCount();i++){
					String typeName = rs.getColumnTypeName(i);
					String fieldName = rs.getColumnName(i);
					String fieldType =Util.changeJDBCTypeToJavaType(typeName);
					if ( fieldType.equals("")){
						fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
					}
					fieldArea.append("	private "+  fieldType + "  " + fieldName +";\r\n");
				}
				out.append(fieldArea.toString());
				for(int i=1;i<=rs.getColumnCount();i++){
					String typeName = rs.getColumnTypeName(i);
					String fieldName = rs.getColumnName(i);
					String fieldType =Util.changeJDBCTypeToJavaType(typeName);
					if ( fieldType.equals(""))
						fieldType = Util.getDataType(rs.getColumnType(i), rs.getScale(i));
					String tmpFieldName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1, fieldName.length());
					if ( tmpFieldName.toLowerCase().equals("id")){
						out.println("	public void set"+tmpFieldName+"Id("+fieldType+"  "+fieldName +"Id) {");
						out.println("		this."+fieldName+"="+fieldName+";");
						out.println("	}");
						out.println("	public "+ fieldType +" get"+tmpFieldName+"Id() {");
						out.println("		return this."+fieldName+"Id;");
						out.println("	}");
					}else{
						out.println("	public void set"+tmpFieldName+"("+fieldType+"  "+fieldName +") {");
						out.println("		this."+fieldName+"="+fieldName+";");
						out.println("	}");
						out.println("	public "+ fieldType +" get"+tmpFieldName+"() {");
						out.println("		return this."+fieldName+";");
						out.println("	}");
					}
				}
				out.println("}");
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	 
}
